import { createRouter, createWebHashHistory } from 'vue-router'

let routes = [
  {
    path: '/',
    redirect: '/login',
    name: 'App',
  },
  {
    path: '/home',
    redirect: '/home',
    name: 'Home',
  },
  {
    path: '/:pathMatch(.*)*',
    name: '404',
    component: () => import('@/views/404'),
  },
]

routes = $globalRouterModules(routes) // 加载模块路由

const $globalRouter = createRouter({
  history: createWebHashHistory(),
  routes,
})

$globalRouter.beforeEach(async (to,from,next) => {
  let token = sessionStorage.getItem('token')
  if (token) {
    next()
  } else {
    if (window.location.href.indexOf('?') > -1) {
      sessionStorage.setItem('token', window.location.href.split('?token=')[1])
      

      next('/home')
    } else {
      window.location.href =
          'http://smartgate.changsha.gov.cn/jczzxfjdjc/manage/mobile/single/loginH5';
     }
  }

})

$globalExpandRouter($globalRouter, routes) // 添加路由扩展

export { $globalRouter }
